{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 泰坦尼克号幸存者预测分析",
    "",
    "本笔记本用于泰坦尼克号幸存者预测项目的交互式数据分析和可视化。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 导入必要的库",
    "import os",
    "import pandas as pd",
    "import numpy as np",
    "import matplotlib.pyplot as plt",
    "import seaborn as sns",
    "",
    "# 导入自定义模块",
    "import sys",
    "sys.path.append('../src')",
    "from data_preprocessing import load_data, explore_data, handle_missing_values, split_data",
    "from feature_engineering import apply_feature_engineering_pipeline",
    "from model_training import train_and_select_best_model, save_model",
    "from model_evaluation import evaluate_model, plot_confusion_matrix, plot_feature_importance, analyze_errors",
    "from visualization import setup_visualization_style, plot_survival_distribution, plot_correlation_heatmap, create_eda_dashboard",
    "",
    "# 设置可视化风格",
    "setup_visualization_style()",
    "",
    "# 确保目录存在",
    "os.makedirs('../data', exist_ok=True)",
    "os.makedirs('../models', exist_ok=True)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 1. 数据准备阶段"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 定义路径",
    "data_dir = '../data'",
    "train_path = os.path.join(data_dir, 'train.csv')",
    "test_path = os.path.join(data_dir, 'test.csv')",
    "",
    "# 加载数据",
    "train_df, test_df = load_data(train_path, test_path)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 查看训练数据的前几行",
    "train_df.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 探索数据基本信息",
    "train_info = explore_data(train_df)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 2. 数据探索与可视化"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 绘制生存状态分布",
    "fig = plot_survival_distribution(train_df)",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 绘制按性别分组的生存率",
    "from visualization import plot_survival_by_feature",
    "fig = plot_survival_by_feature(train_df, 'Sex')",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 绘制按船舱等级分组的生存率",
    "fig = plot_survival_by_feature(train_df, 'Pclass')",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 绘制年龄分布",
    "from visualization import plot_age_distribution_by_survival",
    "fig = plot_age_distribution_by_survival(train_df)",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 绘制票价分布",
    "from visualization import plot_fare_distribution_by_survival",
    "fig = plot_fare_distribution_by_survival(train_df)",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 绘制特征相关性热力图",
    "# 首先选择数值型特征",
    "numeric_cols = train_df.select_dtypes(include=[np.number]).columns",
    "fig = plot_correlation_heatmap(train_df[numeric_cols])",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 创建EDA仪表板",
    "fig = create_eda_dashboard(train_df)",
    "plt.tight_layout()",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 3. 数据预处理阶段"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 处理缺失值",
    "train_df_processed = handle_missing_values(train_df)",
    "",
    "# 检查处理后的缺失值情况",
    "print('处理后的缺失值情况:')",
    "print(train_df_processed.isnull().sum())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 4. 特征工程"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 应用特征工程流程",
    "train_df_fe, encoders, scaler = apply_feature_engineering_pipeline(train_df_processed, is_train=True)",
    "",
    "# 查看处理后的数据",
    "print('处理后的数据形状:, train_df_fe.shape)",
    "train_df_fe.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 5. 数据集划分"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 分割数据集",
    "X_train, X_val, y_train, y_val = split_data(train_df_fe)",
    "",
    "print('训练集特征形状:, X_train.shape)",
    "print('验证集特征形状:', X_val.shape)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 6. 模型训练与评估"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 训练并选择最佳模型",
    "best_model, evaluations = train_and_select_best_model(X_train, y_train, X_val, y_val)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 在验证集上评估最佳模型",
    "val_metrics = evaluate_model(best_model, X_val, y_val)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 绘制混淆矩阵",
    "y_val_pred = best_model.predict(X_val)",
    "fig = plot_confusion_matrix(y_val, y_val_pred, title='Confusion Matrix')",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 绘制特征重要性（仅适用于有feature_importances_属性的模型）",
    "if hasattr(best_model, 'feature_importances_'):",
    "    fig = plot_feature_importance(best_model, X_train.columns, title='Feature Importance')",
    "    plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 分析错误预测",
    "error_samples, error_analysis = analyze_errors(best_model, X_val, y_val, X_train.columns)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 7. 保存模型"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 保存最佳模型",
    "models_dir = '../models'",
    "model_path = os.path.join(models_dir, 'best_model.pkl')",
    "save_model(best_model, model_path)",
    "",
    "# 保存编码器和标准化器",
    "import joblib",
    "joblib.dump(encoders, os.path.join(models_dir, 'encoders.pkl'))",
    "joblib.dump(scaler, os.path.join(models_dir, 'scaler.pkl'))",
    "",
    "print(f'最佳模型已保存到: {model_path}')",
    "print(f'验证集准确率: {val_metrics['accuracy']:.4f}')",
    "print(f'验证集F1分数: {val_metrics['f1']:.4f}'')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 8. 对测试数据进行预测"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 处理测试数据",
    "test_df_processed = handle_missing_values(test_df)",
    "",
    "# 应用特征工程（使用训练好的编码器和标准化器）",
    "test_df_fe, _, _ = apply_feature_engineering_pipeline(test_df_processed, is_train=False, encoders=encoders, scaler=scaler)",
    "",
    "# 确保测试集和训练集的特征顺序一致",
    "test_df_fe = test_df_fe[X_train.columns]",
    "",
    "# 使用最佳模型进行预测",
    "test_predictions = best_model.predict(test_df_fe)",
    "",
    "# 创建提交文件",
    "submission = pd.DataFrame({",
    "    'PassengerId': test_df['PassengerId'],",
    "    'Survived': test_predictions",
    "})",
    "",
    "# 保存提交文件",
    "submission_path = os.path.join(data_dir, 'submission.csv')",
    "submission.to_csv(submission_path, index=False)",
    "",
    "print(f'预测结果已保存到: {submission_path}')",
    "print('预测结果预览:)",
    "submission.head()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}